package com.tang.service;
import java.util.Date;
import java.util.List;

import com.tang.Dao.BookDao;
import com.tang.pojo.*;
import com.tang.Dao.OrderDao;
import com.tang.Dao.OrderItemDao;

public class OrderServiceImpl implements OrderService{
    @Override
    public int createOrder(Cart cart, int userId) {
        OrderDao odo=new OrderDao();
        BookDao bd=new BookDao();
        Date date=new Date();
        String nowTime=date.toString();
        odo.saveOrder(cart.getPriceTotal(),nowTime,0,userId);
        int orderId=odo.queryIdByTime(nowTime).getId();
        OrderItemDao oid=new OrderItemDao();
        for(CartItems cit:cart.getItems()){
            oid.saveOrderItem(cit.getName(),cit.getCount(),cit.getPrice(),cit.getPriceTotal(),orderId);
            Book book=bd.queryByName(cit.getName());
            book.setSale(book.getSale()+cit.getCount());
            book.setStock(book.getStock()-cit.getCount());
            bd.update(book,book.getId());
        }
        return orderId;
    }

    @Override
    public List<Order> showAllOrders() {
        OrderDao od=new OrderDao();
        return od.queryAllOrders();
    }

    @Override
    public void sendStock(int id) {
        OrderDao od=new OrderDao();
        od.sendOrder(id);
    }

    @Override
    public List<OrderItem> showDetails(int orderId) {
        OrderItemDao oid=new OrderItemDao();
        return oid.queryItem(orderId);
    }

    @Override
    public List<Order> showMyOrders(int userID) {
        OrderDao od=new OrderDao();
        return od.queryByUserID(userID);
    }

    @Override
    public void receiveBook(int id) {
        OrderDao od=new OrderDao();
        od.receive(id);
    }

}
